import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;


/**
 *  Keeps global values  
 */
public class Structure {
	public ArrayList<Integer> DiffSet;
	public ArrayList<Integer> FreqSeq;
	public ArrayList<Integer> OrigSet;
	
	public Structure(ArrayList<Integer> Freq) {
		FreqSeq = Freq;
		DiffSet = FreqToDif(FreqSeq);
		checkFreq();
	}
	
	public Structure(int[] array) {
		FreqSeq = new ArrayList<>();
		for (int i = 0; i < array.length; i++) {
			FreqSeq.add(array[i]);
		}
		DiffSet = FreqToDif(FreqSeq);
		checkFreq();
	}
	
	
	public ArrayList<Integer> FreqToDif(ArrayList<Integer> freq) {
		ArrayList<Integer> res = new ArrayList<>();
		for (int i = 0; i < freq.size(); i++) {
			int count = freq.get(i);
			for (int j = 0; j < count; j++) {
				res.add(i);
			}
		}
		Collections.sort(res);
		return res;
	}
	
	public ArrayList<Integer> DifToFreq(ArrayList<Integer> dif) {
		Collections.sort(dif); 
		Iterator<Integer> it = dif.iterator();
		int size = dif.get(dif.size());
		ArrayList<Integer> res = new ArrayList<>(size);
		for (int i = 0; i < size; i++) {
			res.add(0);
		}
		while (it.hasNext()) {
			int temp = it.next();
			res.set(temp, res.get(temp)+1);
		}	
		return res;
	}
	
	/*
	 * Runs every test for input
	 */
	private boolean checkFreq() {
		if (FreqSeq.get(0) > 0) {
			System.err.println("non-zero first element!");
			return false;
		}
		if (!checkSize()) {
			System.err.println("Size is not C(x,2)");
			return false;
		}
		if (DiffSet.size() < 3) {
			System.err.println("Size must be at least 3");
		}
		return true;
	}
	
	private boolean checkSize() { // checks if size is Combination of pairs of Integers
		int size = DiffSet.size();
		int sqr = (int)Math.floor(Math.sqrt(2*size)); 
//		size = n*(n-1)/2 
//		System.out.println(size*2+" == "+sqr+"*"+(sqr+1));
		return sqr*(sqr+1) == size*2; 
	}
}
